package com.itheima.health.controller;

import com.itheima.health.common.MessageConst;
import com.itheima.health.dto.ValidateCodeDTO;
import com.itheima.health.entity.Result;
import com.itheima.health.util.GuoYangYun;
import com.itheima.health.util.ValidateCodeUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.HashOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 验证码Controller
 */
@RestController
@RequestMapping("/mobile/validateCode")
@Slf4j
@Api(tags = "手机端验证码接口")
public class MobileValidateCodeController {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     *
     * @param validateCodeDTO 接收验证码对象
     * @return
     */
    @PostMapping("/send")
    public Result send(ValidateCodeDTO validateCodeDTO){
        //生成验证码
        String code =ValidateCodeUtils.generateValidateCode(4).toString();
        log.info(validateCodeDTO.getTelephone()+"验证码："+code);

        //获取当前时间毫秒值
        long now = System.currentTimeMillis();

        //将验证码和时间毫秒值存入redis中
        String s = now+"_"+code;
        redisTemplate.opsForValue().set(validateCodeDTO.getTelephone(),s);

        GuoYangYun.send(validateCodeDTO.getTelephone(),code);

        return new Result(true, MessageConst.SEND_VALIDATECODE_SUCCESS);
    }

}
